//
//  PopoverAnimation.swift
//  MyWeiboApp
//
//  Created by 胡海波 on 15/7/1.
//  Copyright © 2015年 胡海波. All rights reserved.
//

import UIKit

class PopoverAnimation: NSObject,UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning {

    var presentedFrame = CGRectZero

    // 是否展现标志
    var isPresented = false
    ///  返回负责转场的控制器对象
    ///
    ///  :param: presented
    ///  :param: presenting
    ///  :param: source     源控制器
    ///
    ///  :returns: 返回UIPresentationController控制器
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {


        let pc =  PopoverViewController(presentedViewController: presented, presentingViewController: presenting)
         
        pc.presentedFrame = presentedFrame
        
        return pc
    }

    // 返回提供Modal 动画的对象
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {

        isPresented = true

        return self
    }

    // 返回提供 Dismissed 动画的对象
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {

        isPresented = false

        return self
    }

    // 动画时长
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {

        return isPresented ? 1.0 : 0.25
    }

    // 转场动画实现函数 -- 一旦实现此函数，默认的动画失效，所有的动画由程序员提供

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {

        let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
        let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)

        print(fromVC)
        print(toVC)

        // 判断是展现还是dismissed
        if isPresented {
            let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
            // 将 toView 添加到容器视图
            transitionContext.containerView()?.addSubview(toView)

            // 动画实现
            toView.transform = CGAffineTransformMakeScale(1.0, 0)
            toView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)

            UIView.animateWithDuration(transitionDuration(transitionContext), delay:0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10.0, options: UIViewAnimationOptions.CurveLinear, animations: {

                toView.transform = CGAffineTransformIdentity

                }, completion: { (_) -> Void in

                    transitionContext.completeTransition(true)
            })
        } else {

            let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
            


            UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in

                fromView.transform = CGAffineTransformMakeScale(1.0, 0.0001)

                }, completion: { (_) -> Void in

                fromView.removeFromSuperview()
                // 结束动画
                transitionContext.completeTransition(true)

            })
            
        }
    }


}
